package com.tencent.wxcloudrun.controller;

import com.nlf.calendar.EightChar;
import com.tencent.wxcloudrun.model.SolarDate;
import com.nlf.calendar.Lunar;
import com.nlf.calendar.Solar;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@AllArgsConstructor
@RestController
@RequestMapping("/five")
public class LittleFiveController {
    @RequestMapping("/getLunarInfoBySolar")
    public Map<String, String> getLunarInfoBySolar(SolarDate solarDate){
        // 年月日时分秒
        Solar solar = Solar.fromYmdHms(
                solarDate.getYear(),
                solarDate.getMonth(),
                solarDate.getDay(),
                solarDate.getHour(),
                solarDate.getMinute(),
                solarDate.getSecond()
        );
        Lunar lunar = solar.getLunar();
        Map<String, String> lunarInfo = new HashMap();
        lunarInfo.put("xingzuo", solar.getXingZuo());
        lunarInfo.put("fullString", lunar.toFullString() + solar.toFullString() + getWuxingInfoBySolar(solarDate).toString());
        lunarInfo.put("animal", lunar.getAnimal());
        lunarInfo.put("eightChar", "" + lunar.getEightChar());
        lunarInfo.put("bazi", "" + lunar.getBaZi());

        return lunarInfo;
    }

    @RequestMapping("/getWuxingInfoBySolar")
    public Map<String, String> getWuxingInfoBySolar(SolarDate solarDate){
        // 年月日时分秒
        Solar solar = Solar.fromYmdHms(
                solarDate.getYear(),
                solarDate.getMonth(),
                solarDate.getDay(),
                solarDate.getHour(),
                solarDate.getMinute(),
                solarDate.getSecond()
        );
        Lunar lunar = solar.getLunar();
        EightChar eightChar = lunar.getEightChar();
        eightChar.getMonthWuXing();
        eightChar.getDayWuXing();
        Map<String, String> wuxingInfo = new HashMap();
        wuxingInfo.put("year", eightChar.getYear() + ": " + eightChar.getYearWuXing());
        wuxingInfo.put("month", eightChar.getMonth() + ": " + eightChar.getMonthWuXing());
        wuxingInfo.put("day", eightChar.getDay() + ": " + eightChar.getDayWuXing());
        wuxingInfo.put("time", eightChar.getTime() + ": " + eightChar.getTimeWuXing());
        return wuxingInfo;
    }

    @RequestMapping("/getHepaiScore")
    public Map<String, String> getHepaiScore(@RequestBody List<SolarDate> solarDateList){
        Map<String, String> result = new HashMap();
        // 校验参数
        if (solarDateList.size() != 2){
            return result;
        }
        SolarDate masterSolarDate = solarDateList.get(0);
        SolarDate slaveSolarDate = solarDateList.get(1);
        double score = 0;
        // 需要保证权重之和等于 1
        double heavenlyStemsWeight = 0.6;
        double earthlyBranchesWeight = 0.4;

        // 获取主要一方的生辰八字
        Solar masterSolar = Solar.fromYmdHms(
                masterSolarDate.getYear(),
                masterSolarDate.getMonth(),
                masterSolarDate.getDay(),
                masterSolarDate.getHour(),
                masterSolarDate.getMinute(),
                masterSolarDate.getSecond()
        );
        Lunar masterLunar = masterSolar.getLunar();
        EightChar masterEightChar = masterLunar.getEightChar();
        System.out.println("master:" + masterLunar.toFullString());

        // 获取次要一方的生辰八字
        Solar slaveSolar = Solar.fromYmdHms(
                slaveSolarDate.getYear(),
                slaveSolarDate.getMonth(),
                slaveSolarDate.getDay(),
                slaveSolarDate.getHour(),
                slaveSolarDate.getMinute(),
                slaveSolarDate.getSecond()
        );
        Lunar slaveLunar = slaveSolar.getLunar();
        EightChar slaveEightChar = slaveLunar.getEightChar();
        System.out.println("slave:" + slaveLunar.toFullString());

        // 天干关系
        int heavenlyStemsRelation = getWuxingRelation(transHeavenlyStemsToFive(masterEightChar.getDayGan()), transHeavenlyStemsToFive(slaveEightChar.getMonthGan()));

//        int heavenlyStemsRelation = getWuxingRelation(masterEightChar.getDayWuXing().substring(0, 1), slaveEightChar.getMonthWuXing().substring(0, 1));
        System.out.println(transHeavenlyStemsToFive(masterEightChar.getDayGan()) + transHeavenlyStemsToFive(slaveEightChar.getMonthGan()) + heavenlyStemsRelation);
        score = score + heavenlyStemsWeight * getScoreByWuxingRelation(heavenlyStemsRelation);
        // 地支关系
        int earthlyBranchesRelation = getWuxingRelation(masterEightChar.getDayWuXing().substring(1, 2), slaveEightChar.getMonthWuXing().substring(1, 2));
        System.out.println(masterEightChar.getDayWuXing().substring(1, 2) + slaveEightChar.getMonthWuXing().substring(1, 2) + earthlyBranchesRelation);
        score = score + earthlyBranchesWeight * getScoreByWuxingRelation(earthlyBranchesRelation);

        result.put("master", masterEightChar.getDay() + "," + masterEightChar.getDayWuXing());
        result.put("slave", slaveEightChar.getMonth() + "," + slaveEightChar.getMonthWuXing());
        result.put("score", "" + score);
        return result;
    }
    // 天干转五行
    public String transHeavenlyStemsToFive(String heavenlyStems){
        switch (heavenlyStems){
            case "甲":
                return "木";
            case "乙":
                return "木";
            case "丙":
                return "火";
            case "丁":
                return "火";
            case "戊":
                return "土";
            case "己":
                return "土";
            case "庚":
                return "金";
            case "辛":
                return "金";
            case "壬":
                return "水";
            case "癸":
                return "水";
            default:
                return "不存在";
        }
    }

    // 地支转五行
    public String transEarthlyBranchesToFive(String earthlyBranches){
        switch (earthlyBranches){
            case "子":
                return "水";
            case "丑":
                return "土";
            case "寅":
                return "木";
            case "卯":
                return "木";
            case "辰":
                return "土";
            case "巳":
                return "火";
            case "午":
                return "火";
            case "未":
                return "土";
            case "申":
                return "金";
            case "酉":
                return "金";
            case "戌":
                return "土";
            case "亥":
                return "水";
            default:
                return "不存在";
        }
    }

    // 根据五行关系计算匹配得分，最高 1 分
    public double getScoreByWuxingRelation(int relation){
        switch (relation){
            case 1:
                return 1;
            case 2:
                return 0.8;
            case 3:
                return 0.5;
            case 4:
                return 0.2;
            case 5:
                return 0.0;
            default:
                return 0.0;
        }
    }
    /* 计算五行关系
    * params：wuxing1，wuxing2
    * return：
    * {
    *   0: 输入错误
    *   1: wuxing1 克 wuxing2
    *   2: wuxing2 生 wuxing1
    *   3: wuxing1 平 wuxing2
    *   4: wuxing1 生 wuxing2
    *   5: wuxing2 克 wuxing1
    * }
    *
    * */
    public int getWuxingRelation(String wuxing1, String wuxing2){
        if(wuxing1.equals(wuxing2)){
            return 3;
        }
        switch (wuxing1){
            case "木":
                switch (wuxing2){
                    case "火":
                        return 4;
                    case "土":
                        return 1;
                    case "金":
                        return 5;
                    case "水":
                        return 2;
                }
            case "火":
                switch (wuxing2){
                    case "木":
                        return 2;
                    case "土":
                        return 4;
                    case "金":
                        return 1;
                    case "水":
                        return 5;
                }
            case "土":
                switch (wuxing2){
                    case "木":
                        return 5;
                    case "火":
                        return 2;
                    case "金":
                        return 4;
                    case "水":
                        return 1;
                }
            case "金":
                switch (wuxing2){
                    case "木":
                        return 1;
                    case "火":
                        return 5;
                    case "土":
                        return 2;
                    case "水":
                        return 4;
                }
            case "水":
                switch (wuxing2){
                    case "木":
                        return 4;
                    case "火":
                        return 1;
                    case "土":
                        return 5;
                    case "金":
                        return 2;
                }
            default:
                return 0;
        }
    }

}
